<template>
  <div class="app-container">
    <searchForm  :formOptions="formOptions"
      @onSearch="onSearch"
      @onReset="onReset"
      :formLabelWidth="formLabelWidth"
      v-show="showSearch"></searchForm>
    <!-- <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form> -->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['campus:workLogSetting:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['campus:workLogSetting:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['campus:workLogSetting:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="workLogSettingList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="工作日志类型" align="center" prop="workLogType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.clr_work_log_type" :value="scope.row.workLogType"/>
        </template>
      </el-table-column>
      <el-table-column label="频率类型" align="center" prop="rate">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cps_rate_type" :value="scope.row.rate"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createBy">
        <!-- <template slot-scope="scope">
          <dict-tag :options="dict.type.${column.dictType}" :value="scope.row.createBy"/>
        </template> -->
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改人" align="center" prop="updateBy">
        <!-- <template slot-scope="scope">
          <dict-tag :options="dict.type.${column.dictType}" :value="scope.row.updateBy"/>
        </template> -->
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['campus:workLogSetting:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['campus:workLogSetting:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改工作提醒频率设置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="工作日志类型" prop="workLogType">
          <el-select v-model="form.workLogType" placeholder="请选择工作日志类型" style="width: 100%;">
            <el-option
              v-for="dict in dict.type.clr_work_log_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="频率类型" prop="rate">
          <el-select v-model="form.rate" placeholder="请选择频率类型"  style="width: 100%;">
            <el-option
              v-for="dict in dict.type.cps_rate_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import searchForm from "@/components/Search/searchForm.vue";
import { getDicts } from "@/api/system/dict/type";
import { listWorkLogSetting, getWorkLogSetting, delWorkLogSetting, addWorkLogSetting, updateWorkLogSetting } from "@/api/campus/workLogSetting";

export default {
  name: "WorkLogSetting",
  dicts: ['clr_work_log_type', 'cps_rate_type'],
  components: { searchForm },
  data() {
    return {
      formLabelWidth: "100px",
      formOptions: [
        {
          label: "工作日志类型",
          prop: "workLogType",
          element: "el-select",
          options: [],
          placeholder: "请选择工作日志类型"
        },
      ],
      rules:{
        workLogType: [
          {
            required: true,
            message: '请选择工作日志类型',
            trigger: "change"
          }
        ],
        rate: [
          {
            required: true,
            message: '请选择频率类型',
            trigger: "change"
          }
        ]
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工作提醒频率设置表格数据
      workLogSettingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      name: []
    };
  },
  created() {
    this.getList();
    this.getLog()
  },
  methods: {
    getLog() {
      getDicts("clr_work_log_type").then(res => {
      this.formOptions[0].options = res.data.map(item => {
        return {
          value: item["dictValue"],
          label: item["dictLabel"]
        };
      });
    });
    },
     // 获取搜索表单提交的数据
     onSearch(val) {
      // 查询条件和分页数据合并
      this.queryParams = Object.assign(this.queryParams, val);
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 重置
    onReset(data) {
      console.log(data);
      this.queryParams.pageNum = 1;
      this.queryParams = Object.assign(this.queryParams, data);
      console.log(this.queryParams);
      this.getList();
    },
    /** 查询工作提醒频率设置列表 */
    getList() {
      this.loading = true;
      listWorkLogSetting(this.queryParams).then(response => {
        this.workLogSettingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.resetForm("form");
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.crsId)
      this.single = selection.length!==1
      this.name = selection.map(item => item.workLogType);
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true;
      this.title = "添加工作日志频率设置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const crsId = row.crsId || this.ids
      getWorkLogSetting(crsId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改工作日志频率设置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.crsId != null) {
            updateWorkLogSetting(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWorkLogSetting(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.resetForm("form");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const crsIds = row.crsId || this.ids;
      this.name = row.workLogType || this.name;
      this.$modal.confirm('是否确认删除工作日志类型为"' +  this.name + '"的数据项？').then(function() {
        return delWorkLogSetting(crsIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
  }
};
</script>
